

#include "HeaderFiles.h"


can_receive_message_struct  usercmd_message;     // (receive) user ctrl cmd message buffer 
can_trasnmit_message_struct userfb_message[2];   // (send)    user ctrl state feedback message buffer 
can_trasnmit_message_struct canobs_message[2];   // (send)    user obsdata message buffer 

/*!
    \brief      can_obsdata_init
    \param[in]  none
    \param[out] none
    \retval     none
*/

void can_obsdata_init(void)
{
    
    // can tx message set --------------------------
	can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &canobs_message[0]);
	can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &canobs_message[1]);
	can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &userfb_message[0]);
	can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &userfb_message[1]);

	canobs_message[0].tx_sfid = CAN_ID_OBS_SEND_X0;
	canobs_message[0].tx_efid = 0;
	canobs_message[0].tx_ft   = CAN_FT_DATA;
	canobs_message[0].tx_ff   = CAN_FF_STANDARD;
	canobs_message[0].tx_dlen = 8;

	canobs_message[1].tx_sfid = CAN_ID_OBS_SEND_X1;
	canobs_message[1].tx_efid = 0;
	canobs_message[1].tx_ft   = CAN_FT_DATA;
	canobs_message[1].tx_ff   = CAN_FF_STANDARD;
	canobs_message[1].tx_dlen = 8;
	
	userfb_message[0].tx_sfid = CAN_ID_USERCTRL_TAR_M1;
	userfb_message[0].tx_efid = 0;
	userfb_message[0].tx_ft   = CAN_FT_DATA;
	userfb_message[0].tx_ff   = CAN_FF_STANDARD;
	userfb_message[0].tx_dlen = 8;
	
	userfb_message[1].tx_sfid = CAN_ID_USERCTRL_TAR_M2;
	userfb_message[1].tx_efid = 0;
	userfb_message[1].tx_ft   = CAN_FT_DATA;
	userfb_message[1].tx_ff   = CAN_FF_STANDARD;
	userfb_message[1].tx_dlen = 8;
    
    // can rx message set FIF1 use to User debug ----------------------
    can_filter_parameter_struct can_filter;
    can_struct_para_init(CAN_INIT_STRUCT, &can_filter);

	can_filter.filter_number      = 3;	
    can_filter.filter_mode        = CAN_FILTERMODE_LIST;
    can_filter.filter_bits        = CAN_FILTERBITS_16BIT;
    can_filter.filter_list_high   = CAN_ID_USERCTRL_REC_M1 << 5;
    can_filter.filter_list_low    = CAN_ID_USERCTRL_REC_M2 << 5;
    can_filter.filter_mask_high   = 0x0000;
    can_filter.filter_mask_low    = 0x0000;  
    can_filter.filter_fifo_number = CAN_FIFO1;
    can_filter.filter_enable      = ENABLE;
    can_filter_init(&can_filter);
   
}


/*!
    \brief      can_obsdata_send
    \param[in]  none
    \param[out] none
    \retval     none
*/

void can_obsdata_send(can_trasnmit_message_struct *transmit_message, int16_t *data)
{	
	transmit_message->tx_data[0] =  (*data & 0x00FF); 
	transmit_message->tx_data[1] = ((*data >> 8) & 0x00FF);
	transmit_message->tx_data[2] =  (*(data + 1) & 0x00FF);
	transmit_message->tx_data[3] = ((*(data + 1) >> 8) & 0x00FF);
	transmit_message->tx_data[4] =  (*(data + 2) & 0x00FF);
	transmit_message->tx_data[5] = ((*(data + 2) >> 8) & 0x00FF);
	transmit_message->tx_data[6] =  (*(data + 3) & 0x00FF);
	transmit_message->tx_data[7] = ((*(data + 3) >> 8) & 0x00FF);
		
	can_message_transmit(CAN0, transmit_message);	
}


/*!
    \brief      can_obsdata_recive
    \param[in]  none
    \param[out] none
    \retval     none
*/

void can_debugcmd_rec(can_receive_message_struct *receive_message)
{

    if(can_receive_message_length_get(CAN0, CAN_FIFO1) != 0)
    {
        can_message_receive(CAN0, CAN_FIFO1, receive_message);	        
    }

}

